package cn.yanghx.pay.service;

import cn.yanghx.pay.constant.PayConstants;
import cn.yanghx.pay.process.PayProcessInterface;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yanghx
 */
@Component
public class PayServiceImpl implements IPayService {

    @Resource
    private List<PayProcessInterface> payProcessList;


    @Override
    public PayProcessInterface wechatAppPay() {
        return getInstance(PayConstants.PayMethodEnum.wechat_app_pay);
    }

    @Override
    public PayProcessInterface wechatMiniProgramPay() {
        return getInstance(PayConstants.PayMethodEnum.wechat_mini_program_pay);
    }


    @Override
    public PayProcessInterface getInstance(PayConstants.PayMethodEnum payMethodEnum) {
        for (PayProcessInterface payProcess : payProcessList) {
            if (payProcess.match(payMethodEnum)) {
                return payProcess;
            }
        }
        throw new RuntimeException("错误。暂不支持的支付方式");
    }
}
